home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / resc.zip / RESCONV / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1992-07-13  |  9KB  |  269 lines

  1. /*
  2.  *
  3.  *  Output procedures.
  4.  *
  5.  */
  6.  
  7. #include "resconv.h"
  8. #include "extdef.h"
  9.  
  10. #include <stdio.h>
  11.  
  12.  
  13. HANDLE   dst_fnum;                       /* output file handle */
  14. uchar   dst_buffer [max_buffer_bytes];  /* output data buffer */
  15. ushort  dst_bytes;                      /* # bytes in data buffer */
  16.  
  17.  
  18. /**************************************************************************
  19.  *                                                                        *
  20.  *  WRITE_DATA                                                            *
  21.  *                                                                        *
  22.  *  This procedure is called to write some data to a file.                *
  23.  *                                                                        *
  24.  **************************************************************************/
  25.  
  26. static flag write_data (HFILE fnum, void ptr buf, ushort count)
  27. {
  28. ushort error;
  29. DWORD written;
  30.  
  31. /*  Write the data, and die if we get an error. */
  32.  
  33. error = WriteFile (fnum, buf, count, &written, NULL);
  34. if ((error == 0) || count != written) {
  35.     printf ("I/O error %ld while writing to data file.\n", GetLastError());
  36.     close_destination_file ();
  37.     terminate_input ();
  38.     exit(1);
  39.     }
  40.  
  41. /*  Success is ours. */
  42.  
  43. return TRUE;
  44. }
  45.  
  46.  
  47. /**************************************************************************
  48.  *                                                                        *
  49.  *  OUTPUT_CONTROL_LINE                                                   *
  50.  *                                                                        *
  51.  *  This procedure is called to output a the current line as a control    *
  52.  *  line.                                                                 *
  53.  *                                                                        *
  54.  **************************************************************************/
  55.  
  56. void output_control_line (uchar ptr string)
  57. {
  58. char    ch;
  59.  
  60. /*
  61.  *  If there isn't enough room in the output buffer for 256 bytes, write
  62.  *  the buffer data out to file.
  63.  */
  64.  
  65. output_newline ();
  66. if (dst_bytes + 256 >= max_buffer_bytes) {
  67.     write_data (dst_fnum, dst_buffer, dst_bytes);
  68.     dst_bytes = 0;
  69.     }
  70.  
  71. /*
  72.  *  Get characters transferring them to the destination buffer until
  73.  *  we see the carriage return. Get the line feed and finish the line.
  74.  */
  75.  
  76. dst_buffer [dst_bytes++] = char_pound_sign;
  77. _fstrncpy (&dst_buffer [dst_bytes], string, string_length (string));
  78. dst_bytes += string_length (string);
  79.  
  80. while ((ch = get_char ()) != 0x0D)
  81.     dst_buffer [dst_bytes++] = ch;
  82. get_char ();
  83. }
  84.  
  85.  
  86. /**************************************************************************
  87.  *                                                                        *
  88.  *  OUTPUT_NEWLINE                                                        *
  89.  *                                                                        *
  90.  *  This procedure is called to output a CR/LF.                           *
  91.  *                                                                        *
  92.  **************************************************************************/
  93.  
  94. void output_newline (void)
  95. {
  96.  
  97. if (dst_bytes + 2 >= max_buffer_bytes) {
  98.     write_data (dst_fnum, dst_buffer, dst_bytes);
  99.     dst_bytes = 0;
  100.     }
  101.  
  102. if (get_comment_string() != NULL) {
  103.     output_token(get_comment_string());
  104.     clear_comment_string();
  105. }
  106.  
  107. dst_buffer [dst_bytes++] = 0x0D;
  108. dst_buffer [dst_bytes++] = 0x0A;
  109. }
  110.  
  111.  
  112. /**************************************************************************
  113.  *                                                                        *
  114.  *  OUTPUT_TAB                                                            *
  115.  *                                                                        *
  116.  *  This procedure is called to output the specified number of tab        *
  117.  *  characters.                                                           *
  118.  *                                                                        *
  119.  **************************************************************************/
  120.  
  121. void output_tab (ushort count)
  122. {
  123. int i;
  124.  
  125. if (dst_bytes + (count * 4) >= max_buffer_bytes) {
  126.     write_data (dst_fnum, dst_buffer, dst_bytes);
  127.     dst_bytes = 0;
  128.     }
  129.  
  130. for (i = 0; i < count * 4; i++)
  131.     dst_buffer [dst_bytes++] = ' ';
  132. }
  133.  
  134.  
  135. /**************************************************************************
  136.  *                                                                        *
  137.  *  OUTPUT_STRING                                                         *
  138.  *                                                                        *
  139.  *  This procedure is called to output a quoted string.                   *
  140.  *                                                                        *
  141.  **************************************************************************/
  142.  
  143. void output_string (uchar ptr string)
  144. {
  145. ushort  length;
  146.  
  147. /*  See if there's enough room left in the output buffer for this string. */
  148.  
  149. length = string_length (string);
  150. if (dst_bytes + length + 3 >= max_buffer_bytes) {
  151.     write_data (dst_fnum, dst_buffer, dst_bytes);
  152.     dst_bytes = 0;
  153.     }
  154.  
  155. /*
  156.  *  Put the string surrounded by double-quotes in the output buffer and
  157.  *  insert a space at the end.
  158.  */
  159.  
  160. dst_buffer [dst_bytes++] = '"';
  161. _fstrncpy (&dst_buffer [dst_bytes], string, length);
  162. dst_bytes += length;
  163. dst_buffer [dst_bytes++] = '"';
  164. dst_buffer [dst_bytes++] = ' ';
  165. }
  166.  
  167.  
  168. /**************************************************************************
  169.  *                                                                        *
  170.  *  OUTPUT_TOKEN                                                          *
  171.  *                                                                        *
  172.  *  This procedure is called to output a token string.                    *
  173.  *                                                                        *
  174.  **************************************************************************/
  175.  
  176. void output_token (uchar ptr string)
  177. {
  178. ushort  length;
  179.  
  180. /*  See if there's enough room left in the output buffer for this string. */
  181.  
  182. length = string_length (string);
  183. if (dst_bytes + length + 1 >= max_buffer_bytes) {
  184.     write_data (dst_fnum, dst_buffer, dst_bytes);
  185.     dst_bytes = 0;
  186.     }
  187.  
  188. /*  Put the string in the output buffer and insert a space at the end. */
  189.  
  190. _fstrncpy (&dst_buffer [dst_bytes], string, length);
  191. dst_bytes += length;
  192. dst_buffer [dst_bytes++] = ' ';
  193. }
  194.  
  195.  
  196. /**************************************************************************
  197.  *                                                                        *
  198.  *  OUTPUT_VALUE                                                          *
  199.  *                                                                        *
  200.  *  This procedure is called to output a value as a string.               *
  201.  *                                                                        *
  202.  **************************************************************************/
  203.  
  204. void output_value (long value)
  205. {
  206. uchar   buffer [40];
  207.  
  208. /*
  209.  *  Convert the value to a string and insert the string in the output
  210.  *  buffer.
  211.  */
  212.  
  213. ltoa (value, buffer, 10);
  214. output_token (buffer);
  215. }
  216.  
  217.  
  218. /**************************************************************************
  219.  *                                                                        *
  220.  *  OPEN_DESTINATION_FILE                                                 *
  221.  *                                                                        *
  222.  *  This procedure is called to open the destination file.                *
  223.  *                                                                        *
  224.  **************************************************************************/
  225.  
  226. flag open_destination_file (uchar ptr dst_fname)
  227. {
  228.  
  229. /*  Open the destination file. */
  230.  
  231. printf ("Opening  %s\n", dst_fname);
  232. dst_fnum = CreateFile(dst_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  233.     FILE_ATTRIBUTE_NORMAL, NULL);
  234. if (dst_fnum == NULL)
  235. {
  236.     printf ("Unable to create: %s, error %ld\n", dst_fname, GetLastError());
  237.     close_destination_file ();
  238.     terminate_input ();
  239.     exit(1);
  240. }
  241. /*  Success. */
  242.  
  243. return TRUE;
  244. }
  245.  
  246.  
  247. /**************************************************************************
  248.  *                                                                        *
  249.  *  CLOSE_DESTINATION_FILE                                                *
  250.  *                                                                        *
  251.  *  This procedure writes out any remaining bytes in the destination      *
  252.  *  file and closes it.                                                   *
  253.  *                                                                        *
  254.  **************************************************************************/
  255.  
  256. void close_destination_file (void)
  257. {
  258. DWORD written;
  259. int error;
  260.  
  261. error = WriteFile (dst_fnum, dst_buffer, dst_bytes, &written, NULL);
  262. if ((error == 0) || dst_bytes != written) {
  263.     printf ("I/O error %ld while writing to data file.\n", GetLastError());
  264.     terminate_input ();
  265.     exit(1);
  266.     }
  267. CloseHandle(dst_fnum);
  268. }
  269.